﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using BusinessLogicLayer;


namespace PresentationLayer
{
    public partial class MaintainDepartmentInformation : System.Web.UI.Page
    {
        MaintainDepartmentInformationControl mdiControl;
        StationeryInventoryEntities1 ent;
        //string userName = "COMM_Head";
        string userName;
        String roleID;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["UserLoggedIn"] == null)
                {
                    Response.Redirect("login.aspx");
                }
                else
                {
                    List<String> loginValues = (List<String>)(Session["UserLoggedIn"]);
                    if (loginValues[6] == "4" || loginValues[6] == "7") //check whether login user's role is equal to Dept Head or store manager
                    {
                        userName = loginValues[3];
                        roleID = loginValues[6];
                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }

                }
                mdiControl = new MaintainDepartmentInformationControl();
                ent = new StationeryInventoryEntities1();

                lblCode.Text = mdiControl.getDepartmentCode(userName).ToString();
                lblName.Text = mdiControl.getDepartmentName(userName).ToString();
                lblHead.Text = mdiControl.getDepartmentHead(userName).ToString();

                if (!IsPostBack)
                {

                    ddlRepName.DataSource = mdiControl.getRepresentativeName(userName);
                    ddlRepName.DataTextField = "EmployeeName";
                    ddlRepName.DataValueField = "EmployeeID";
                    ddlRepName.DataBind();
                    //if the user is department head or store manager no need to select 
                    if (roleID != "4" || roleID != "7")
                    {
                        try
                        {
                            ddlRepName.Items.FindByText(mdiControl.getUpdatedRepresentativeName(userName).ToString()).Selected = true;
                        }
                        catch (Exception ex)
                        {

                        }
                    }
                    ddlCollectionPoint.DataSource = mdiControl.getCollectionPoint();
                    ddlCollectionPoint.DataTextField = "CollectionPointName";
                    ddlCollectionPoint.DataValueField = "CollectionPointID";
                    ddlCollectionPoint.DataBind();
                    ddlCollectionPoint.Items.FindByText(mdiControl.getUpdatedCollectionPoint(userName).ToString()).Selected = true;
                    int cId = Convert.ToInt32(ddlCollectionPoint.SelectedValue);
                    DateTime collTime = (DateTime)mdiControl.getCollectionTimeByCPID(cId);
                    lblTime.Text = collTime.ToShortTimeString();

                    hiddenRepId.Value = ddlRepName.SelectedValue;

                }
            }
            catch (Exception ex)
            { 
            
            }
            
        }

        protected void ddlCollectionPoint_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                lblChanges.Visible = false;
                int collPointID = Convert.ToInt32(ddlCollectionPoint.SelectedValue.ToString());
                DateTime collTime = (DateTime)mdiControl.getCollectionTimeByCPID(collPointID);
                lblTime.Text = collTime.ToShortTimeString();
            }
            catch(Exception ex)
            { 
                
            }
            
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string deptCode = lblCode.Text;
                int repName = Convert.ToInt32(ddlRepName.SelectedItem.Value);
                int collPoint = Convert.ToInt32(ddlCollectionPoint.SelectedItem.Value);
                mdiControl.updateInfo(deptCode, repName, collPoint);
                lblChanges.Visible = true;
            }
            catch
            {
                
            }
            
           
        }

        protected void ddlRepName_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblChanges.Visible = false;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                ddlRepName.ClearSelection();
                ddlRepName.Items.FindByText(mdiControl.getUpdatedRepresentativeName(userName).ToString()).Selected = true;
                ddlCollectionPoint.ClearSelection();
                ddlCollectionPoint.Items.FindByText(mdiControl.getUpdatedCollectionPoint(userName).ToString()).Selected = true;
            
            }
            catch (Exception ex)
            { 
            
            }
            
        }

    }
}